<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>canvas 文字</title>
    <style>
        canvas {
            border: 2px solid red;
        }
    </style>
</head>

<body>
    <canvas id="myCanvas" width="500" height="500"></canvas>

    <script>
        var c = document.getElementById('myCanvas');
        var ctx = c.getContext('2d');
        ctx.font = '30px Arial';
        ctx.fillText('小黑', 100, 200);
        ctx.strokeText('小黑', 100, 300);
        var text = ctx.measureText('佛挡杀佛');
        console.log('text:', text.width);
    </script>
</body>

</html>